package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.distribution.DistributionSubstations;
import com.ruoyi.system.domain.distribution.PlatformTerrace;
import com.ruoyi.system.service.IDistributionSubstationsService;
import com.sun.org.apache.bcel.internal.generic.LSUB;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.system.service.IPlatformTerraceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2021-03-17
 */
@RestController
@RequestMapping("/system/terrace")
public class PlatformTerraceController extends BaseController
{
    @Autowired
    private IPlatformTerraceService platformTerraceService;
    @Autowired
    private IDistributionSubstationsService distributionSubstationsService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:terrace:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlatformTerrace platformTerrace)
    {
        startPage();
        List<PlatformTerrace> list = platformTerraceService.selectPlatformTerraceList(platformTerrace);
        return getDataTable(list);
    }

    @GetMapping("/findSum")
    public TableDataInfo findSum(PlatformTerrace platformTerrace){
        List<PlatformTerrace> list = platformTerraceService.selectPlatformTerraceList(platformTerrace);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:terrace:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PlatformTerrace platformTerrace)
    {
        List<PlatformTerrace> list = platformTerraceService.selectPlatformTerraceList(platformTerrace);
        ExcelUtil<PlatformTerrace> util = new ExcelUtil<PlatformTerrace>(PlatformTerrace.class);
        return util.exportExcel(list, "terrace");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:terrace:query')")
    @GetMapping(value = "/{terraceId}")
    public AjaxResult getInfo(@PathVariable("terraceId") Integer terraceId)
    {
        return AjaxResult.success(platformTerraceService.selectPlatformTerraceById(terraceId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:terrace:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformTerrace platformTerrace)
    {
        return toAjax(platformTerraceService.insertPlatformTerrace(platformTerrace));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:terrace:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformTerrace platformTerrace)
    {
        return toAjax(platformTerraceService.updatePlatformTerrace(platformTerrace));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:terrace:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{terraceIds}")
    public AjaxResult remove(@PathVariable Integer[] terraceIds)
    {
        return toAjax(platformTerraceService.deletePlatformTerraceByIds(terraceIds));
    }

    @GetMapping(value = "/findAllSum")
    public int findAllSum(DistributionSubstations distributionSubstations){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
//        Long userId = (long)38;
        distributionSubstations.setUserId(userId);
        int allSum = distributionSubstationsService.findAllSum(distributionSubstations);
        return allSum;
    }

    @GetMapping(value = "/findAllSumTwo")
    public TableDataInfo findAllSumTwo(DistributionSubstations distributionSubstations){
        startPage();
        List<DistributionSubstations> allSum = distributionSubstationsService.findAllSumTwo(distributionSubstations);
        return getDataTable(allSum);
    }

    @GetMapping("/exportTwo")
    public AjaxResult export(DistributionSubstations distributionSubstations)
    {
        List<DistributionSubstations> list = distributionSubstationsService.findAllSumTwo(distributionSubstations);
        ExcelUtil<DistributionSubstations> util = new ExcelUtil<DistributionSubstations>(DistributionSubstations.class);
        return util.exportExcel(list, "substations");
    }
}
